<template>
  <el-form ref="settingForm" :rules="rules" :model="settingForm" label-width="140px">
    <div class="setting-title">设置</div>
    <el-form-item label="网校域名">
      <el-input v-model="settingForm.name" placeholder="请输入网站域名" style="width:500px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitBtn">保 存</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
  import {getTenantId, getUserName} from '@/utils/auth'
  import {getDomainName, saveOrUpdateDomainName} from '@/api/distribution/agencysettingDomain'

  export default {
    data() {
      return {
        settingForm: {
          name: ''
        },
        rules: {
          name: [
            {required: true, message: '请输入网站域名', trigger: 'blur'},
            {
              required: true,
              pattern: "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)",
              message: '请输入正确的域名',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    created() {
      this.getData()
    },
    methods: {
      getData() {
        getDomainName(getTenantId()).then(res => {
          if (res.data.item) this.settingForm = res.data.item
        })
      },
      submitBtn() {
        this.$refs.settingForm.validate(valid => {
          if (valid) {
            if (!this.settingForm.id) {
              this.settingForm.creator = getUserName()
              this.settingForm.tenantId = getTenantId()
            }
            saveOrUpdateDomainName(this.settingForm).then(res => {
              this.getData()
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .setting-title {
    margin-top: 20px;
    margin-left: 20px;
    padding: 8px 20px;
    font-size: 18px;
    line-height: 30px;
    color: #666;
  }
</style>
